package com.tanhua.dubbo.api;

import com.tanhua.domain.mongo.Friend;
import com.tanhua.domain.mongo.RecommendUser;
import com.tanhua.domain.mongo.UserLike;
import com.tanhua.domain.vo.PageResult;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

@Service
public class UserLikeApiImpl implements UserLikeApi {
    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public int eachLoveCount(Long userId) {
        Query query = new Query();
        query.addCriteria(Criteria.where("userId").is(userId));
        int eachLoveCount = (int) mongoTemplate.count(query, Friend.class);
        return eachLoveCount;
    }

    @Override
    public int fanCount(Long userId) {
        Query query = new Query();
        query.addCriteria(Criteria.where("likeUserId").is(userId));
        int fanCount = (int) mongoTemplate.count(query, UserLike.class);
        return fanCount;
    }

    @Override
    public int loveCount(Long userId) {
        Query query = new Query();
        query.addCriteria(Criteria.where("userId").is(userId));
        int loveCount = (int) mongoTemplate.count(query, UserLike.class);
        return loveCount;
    }

    @Override
    public PageResult findeachLoveList(Long userId, Integer page, Integer pagesize) {
        Query query = new Query();
        query.addCriteria(Criteria.where("userId").is(userId));
        query.with(Sort.by(Sort.Order.desc("created")));
        query.with(PageRequest.of(page-1, pagesize));
        List<Friend> friends = mongoTemplate.find(query, Friend.class);
        System.out.println("Friends"+friends);
        long count = mongoTemplate.count(query, Friend.class);
        int pages = (int) Math.ceil(count*1.0/pagesize);
        return new PageResult((int) count,pagesize,pages,page,friends);
    }



    @Override
    public PageResult findLoveList(Long userId, Integer page, Integer pagesize) {
        Query query = new Query();
        query.addCriteria(Criteria.where("userId").is(userId));
        query.with(Sort.by(Sort.Order.desc("created")));
        query.with(PageRequest.of(page-1, pagesize));
        List<UserLike> userLikes = mongoTemplate.find(query, UserLike.class);
        long count = mongoTemplate.count(query, UserLike.class);
        int pages = (int) Math.ceil(count*1.0/pagesize);

        return new PageResult((int) count,pagesize,pages,page,userLikes);
    }

    @Override
    public PageResult findFanList(Long userId, Integer page, Integer pagesize) {

        Query query = new Query();
        query.addCriteria(Criteria.where("likeUserId").is(userId));
        query.with(Sort.by(Sort.Order.desc("created")));
        query.with(PageRequest.of(page-1, pagesize));
        List<UserLike> userLikes = mongoTemplate.find(query, UserLike.class);
        long count = mongoTemplate.count(query, UserLike.class);
        int pages = (int) Math.ceil(count*1.0/pagesize);

        return new PageResult((int) count,pagesize,pages,page,userLikes);
    }

    @Override
    public void fansLike(Long userId, Long fanId) {
        Query query = new Query();
        query.addCriteria(Criteria.where("likeUserId").is(userId).and("userId").is(fanId));
        mongoTemplate.remove(query,UserLike.class);
    }


    @Override
    public Integer findScore(Long toUserId, Long userId) {
        Query query = new Query();
        query.addCriteria(Criteria.where("toUserId").is(toUserId).and("userId").is(userId));
        RecommendUser recommendUser = mongoTemplate.findOne(query, RecommendUser.class);
        if (recommendUser != null) {
            return recommendUser.getScore().intValue();
        }
        return 95;
    }
}
